package com.beiing.tianshuai.tianshuai.mine.ui;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.ActivitiesPublishAdapter;
import com.beiing.tianshuai.tianshuai.entity.ActivitiesPublishBean;
import com.beiing.tianshuai.tianshuai.fragment.BaseLazyLoadFragment;
import com.beiing.tianshuai.tianshuai.mine.presenter.ActivitiesPublishPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.ActivitiesPublishViewImpl;
import com.beiing.tianshuai.tianshuai.widget.FooterView;
import com.beiing.tianshuai.tianshuai.widget.TranslucentScrollView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivitiesPublishFragment extends BaseLazyLoadFragment implements ActivitiesPublishViewImpl {

    public static final int                 PAGE_SIZE       = 10;
    private int                             mLastItemIndex  = 0;
    private View                            mContentView;
    private Context                         mContext;
    private String                          mType;
    private String                          mUid;
    private ActivitiesPublishAdapter        mAdapter;
    private ActivitiesPublishPresenter      mPresenter;
    private boolean                         isLoadFinished;
    private List<ActivitiesPublishBean.DataBean.ActiveBean> number;

    @BindView(R.id.rv_activities_list)
    RecyclerView mRvActivitiesList;
    @BindView(R.id.srl_my_refresh_layout)
    SwipeRefreshLayout mSrlMyRefreshLayout;
    @BindView(R.id.txt_empty_warning)
    TextView mTxtEmptyWarning;
    @BindView(R.id.tsv_scroll_view)
    TranslucentScrollView mScrollView;

    public static ActivitiesPublishFragment newInstance(String uid, String type) {
        ActivitiesPublishFragment fragment = new ActivitiesPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uid", uid);
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.fragment_activities_publish, container, false);
            mUnbinder = ButterKnife.bind(this, mContentView);
            mContext = getActivity();
            Bundle arguments = getArguments();
            if (arguments != null) {
                mUid = arguments.getString("uid");
                mType = arguments.getString("type");
            }
            isPrepared = true;
            initRecyclerView();
            initListener();
            mPresenter = new ActivitiesPublishPresenter(this);
            mPresenter.getActivitiesResult(mUid, mType, mLastItemIndex, PAGE_SIZE);
        }
        return mContentView;
    }

    private void initRecyclerView() {
        mAdapter = new ActivitiesPublishAdapter(mContext, mType);
        mRvActivitiesList.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRvActivitiesList.setNestedScrollingEnabled(false);
        mRvActivitiesList.setLayoutManager(linearLayoutManager);

        mAdapter.setOnItemClickListener(new ActivitiesPublishAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //点击事件

            }
        });
    }

    private void initListener() {
        // 刷新
        mSrlMyRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

        // 根据ScrollView滑动改变Toolbar背景色
        mScrollView.setOnScrollChangedListener(new TranslucentScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt, Boolean isBottom) {
                if (isBottom) {
                    loadMoreData();
                }
            }
        });
    }

    private void refreshData() {
        isLoadFinished = false;
        mLastItemIndex = 0;
        mAdapter.setFooterViewStatus(FooterView.MORE);
        mAdapter.clearData();
        mAdapter.notifyDataSetChanged();
        mPresenter.getActivitiesResult(mUid, mType, mLastItemIndex, PAGE_SIZE);
    }

    private void loadMoreData() {
        if (!isLoadFinished && mAdapter.getFooterView().getStatus() != FooterView.LOADING) {
            mPresenter.getActivitiesResult(mUid, mType, mLastItemIndex, PAGE_SIZE);
            mAdapter.setFooterViewStatus(FooterView.LOADING);
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || isLoadedOnce) {
            return;
        }
        mPresenter.getActivitiesResult(mUid, mType, mLastItemIndex, PAGE_SIZE);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onRequestSuccess(ActivitiesPublishBean bean) {
        number = bean.getData().getActive();
        int size = number.size();
        if (size == 0) {
            isLoadFinished = true;
            mAdapter.setFooterStatus(FooterView.NO_MORE);
            mAdapter.setFooterViewStatus(FooterView.NO_MORE);
        } else if (size > 0 && size < PAGE_SIZE) {
            isLoadFinished = true;
            mAdapter.appendData(number);
            mAdapter.setFooterStatus(FooterView.NO_MORE);
            mAdapter.setFooterViewStatus(FooterView.NO_MORE);
        } else {
            mAdapter.appendData(number);
            mAdapter.setFooterStatus(FooterView.MORE);
            mAdapter.setFooterViewStatus(FooterView.MORE);
        }
        mAdapter.notifyDataSetChanged();
        if (mTxtEmptyWarning != null) {
            if (mAdapter.getItemCount() <= 0) {
                mTxtEmptyWarning.setVisibility(View.VISIBLE);
            } else {
                mTxtEmptyWarning.setVisibility(View.GONE);
            }
        }
        mLastItemIndex += size;
        isLoadedOnce = true;
        if (mSrlMyRefreshLayout != null && mSrlMyRefreshLayout.isRefreshing()) {
            mSrlMyRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常");
        if (mSrlMyRefreshLayout != null && mSrlMyRefreshLayout.isRefreshing()) {
            mSrlMyRefreshLayout.setRefreshing(false);
        }
        FooterView footerView = mAdapter.getFooterView();
        if (footerView != null && footerView.getStatus() == FooterView.LOADING) {
            footerView.setStatus(FooterView.HIDE);
        }
    }
}
